package com.guslegend.controller;

import com.google.code.kaptcha.Producer;
import com.guslegend.controller.request.SendCodeRequest;
import com.guslegend.enums.BizCodeEnum;
import com.guslegend.enums.SendCodeEnum;
import com.guslegend.service.NotifyService;
import com.guslegend.utils.CommonUtils;
import com.guslegend.utils.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/notify/v1")
public class NotifyController {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private Producer captchaProducer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final long CAPTCHA_CODE_EXPIRED=1000*10*60;

    /**
     * 生成验证码
     * @return
     */
    @RequestMapping("captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){

        String captchaText=captchaProducer.createText();
        log.info("验证码内容:{}",captchaText);

        //存储redis,配置过期时间,jedis/lettuce
        redisTemplate.opsForValue().set(getCaptcha(request),captchaText,CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS);

        BufferedImage bufferedImage=captchaProducer.createImage(captchaText);

        try (ServletOutputStream outputStream= response.getOutputStream()){
            ImageIO.write(bufferedImage,"jpg",outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.info("获取流出错:{}",e.getMessage());
        }
    }

    private String getCaptcha(HttpServletRequest request){

        String ip= CommonUtils.getIpAddr(request);
        String userAgent=request.getHeader("User-Agent");
        String key="account-service:captcha:"+CommonUtils.MD5(ip+userAgent);
        log.info("验证码key:{}",key);
        return key;
    }

    /**
     * 发送短信验证码
     * @return
     */
    @PostMapping("send_code")
    public JsonData sendCode(@RequestBody SendCodeRequest sendCodeRequest,HttpServletRequest request){

        String key=getCaptcha(request);
        String cacheCaptcha=redisTemplate.opsForValue().get(key);
        String captcha=sendCodeRequest.getCaptcha();
        if (captcha!=null&&cacheCaptcha!=null&&cacheCaptcha.equalsIgnoreCase(captcha)){
            //成功
            redisTemplate.delete(key);
            JsonData jsonData= notifyService.sendCode(SendCodeEnum.USER_REGISTER,sendCodeRequest.getTo());
            return jsonData;
        }else{
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR );
        }

    }
}
